﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PropertyManagement.Core.DataAccess;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Core {
    public class SearchManage {
        public IEnumerable<SystemUser> LoadAllSystemUser( ) {
            //throw new NotImplementedException();
            return SystemUserDataAccess.LoadAll( );
        }

        public IEnumerable<HouseOwner> LoadAllHouseOwner( ) {

            //throw new NotImplementedException( );
            return HouseOwnerDataAccess.LoadAll( );
        }

        public IEnumerable<HouseOwner> SearchHouseOwnerByHouseCode(string houseCode) {

            //throw new NotImplementedException( );
            return HouseOwnerDataAccess.SearchByHouseCode(houseCode);
        }

        public IEnumerable<HouseOwner> SearchHouseOwnerByHouseCodeOrOnwerName(string houseCode, string ownerName) {
            return HouseOwnerDataAccess.SearchByHouseCodeAndName(houseCode, ownerName);
        }

        public IEnumerable<PropertyManagementFeeBill> SearchAllUnPaidBillsByHouseID(int houseId) {

            //throw new NotImplementedException( );
            return PropertyManagementFeeBillDataAccess.Search(houseId, null, null, null, false);
        }




        public IEnumerable<HistoryInstrumentBill> SearchHistoryInstrumentBills(DateTime start, DateTime end,
                                                                               BillType billType) {
            return ViewsDataAccess.SearchHistroyInstrumentBills(billType, start, end);
        }
        public IEnumerable<HistoryManagementBill> SearchHistoryManagementBills(DateTime start, DateTime end,
                                                                              BillType billType) {
            return ViewsDataAccess.SearchHistoryManagementBills(billType, start, end);
        }

        public static IEnumerable<MaintainView> SearchHistoryMaintains(bool? status, DateTime sTime, DateTime eTime) {
            return ViewsDataAccess.SearchHistoryMaintains(status, sTime, eTime);
        }

        public static IEnumerable<HistoryBill> SearchUnPaidHistoryBillsByHouse(int houseId) {
            return ViewsDataAccess.SearchHistoryBillByHouse(houseId, null, null).Where(b => !b.IsCompletedPaid);
        }

        public static IEnumerable<HistoryBill> SearchUnPaidHistoryBillsByHouse(int houseId,DateTime? mouth) {
            return ViewsDataAccess.SearchHistoryBillByHouse(houseId, false, mouth);
        }

        public IEnumerable<HistoryInstrumentBill> SearchHistoryInstrumentBills(DateTime month,
                                                                              BillType billType) {
            return ViewsDataAccess.SearchHistroyInstrumentBills(billType, month);
        }
        public IEnumerable<HistoryManagementBill> SearchHistoryManagementBills(DateTime month,
                                                                             BillType billType) {
            return ViewsDataAccess.SearchHistoryManagementBills(billType, month);
        }

        public static IEnumerable<PropertyManagementFeeReport> GetFeeBillsReporting(DateTime startDate, DateTime endDate) {
            var histroyBills = ViewsDataAccess.SearchHistoryBillByHouse(null, true,startDate, endDate);
            List<PropertyManagementFeeReport> reposts = new List<PropertyManagementFeeReport>( );
            foreach (var bill in histroyBills.OrderBy(b => b.HouseCode)) {
                var r = reposts.FirstOrDefault(o => o.HouseCode == bill.HouseCode);
                if (r == null) {
                    r = new PropertyManagementFeeReport {
                        HouseCode = bill.HouseCode,
                        Apartment = bill.ApartmentName,
                        OwnerName = bill.OwnerName
                    };
                    reposts.Add(r);
                }
                if (bill.BillType == BillType.GasFee) {
                    r.GasFee += bill.CallableMoney;
                    r.GasUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.ElectricFee) {
                    r.ElectricFee += bill.CallableMoney;
                    r.ElectricUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.WaterFee) {
                    r.WaterFee += bill.CallableMoney;
                    r.WaterUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.ManagementFee) {
                    r.ManagementFee += bill.CallableMoney;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else {
                    r.SanitationFee += bill.CallableMoney;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                }


            }
            return reposts;
        }

        public static IEnumerable<PropertyManagementFeeReport> GetFeeBillsReporting(DateTime mouth) {
            var histroyBills = ViewsDataAccess.SearchHistoryBillByHouse(null, true,mouth);
            List<PropertyManagementFeeReport> reposts = new List<PropertyManagementFeeReport>( );
            foreach (var bill in histroyBills.OrderBy(b => b.HouseCode)) {
                var r = reposts.FirstOrDefault(o => o.HouseCode == bill.HouseCode);
                if (r == null) {
                    r = new PropertyManagementFeeReport {
                        HouseCode = bill.HouseCode,
                        Apartment = bill.ApartmentName,
                        OwnerName = bill.OwnerName
                    };
                    reposts.Add(r);
                }
                if (bill.BillType == BillType.GasFee) {
                    r.GasFee += bill.CallableMoney;
                    r.GasUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.ElectricFee) {
                    r.ElectricFee += bill.CallableMoney;
                    r.ElectricUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.WaterFee) {
                    r.WaterFee += bill.CallableMoney;
                    r.WaterUsedNumber += bill.FeeNumber;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else if (bill.BillType == BillType.ManagementFee) {
                    r.ManagementFee += bill.CallableMoney;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                } else {
                    r.SanitationFee += bill.CallableMoney;
                    r.OverDateFee += bill.OverdueFee;
                    r.TotalFee += bill.CallableMoney + bill.OverdueFee;
                }


            }
            return reposts;
        }
    }
}
